var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");

var treasure = {
  go_page_from_home() {
    // utils.click_point.apply(
    //   utils,
    //   utils.get_size_point(point_map["首页赚钱"], 1080, 2340)
    // );
    var logger = utils.create_logger("treasure go_page_from_home: ");
    logger();
    var interval = utils.get_random_interval(3, 0, 2, 1000);
    logger("等待--" + interval / 1000 + "--秒后开始判断是否开宝箱");
    sleep(interval);
    this.init();
  },
  init() {
    var logger = utils.create_logger("treasure init: ");
    logger();
    var ocr_text = ocr_utils.screen_exists_text_one(
      ["开宝箱得金币", "点击立得"],
      true
    );
    if (ocr_text) {
      logger("找到--开宝箱得金币或者点击立得--");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var ocr_text = ocr_utils.screen_exists_text_one("看广告视频再赚", false);
      if (ocr_text) {
        logger("找到--看广告视频再赚--");
        utils.click_ocr_text(ocr_text);
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        this.run();
      } else {
        logger("没找到--看广告视频再赚--");
        back();
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        this.after();
      }
    } else {
      logger("没找到--开宝箱得金币--");
      this.after();
    }
  },
  run() {
    ad.init();
    this.after();
  },
  after() {
    console.log("treasure after");
    if (utils.click_text_starts_with("开心收下")) {
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    }
    this.next();
  },
  next() {
    console.log("treasure next");
  },
};
module.exports = treasure;
